import { customFetch, sendSSE } from '../utils/tools';
import {
  HistoryReq,
  HistoryRes,
  CreateReq,
  CreateRes,
  ChatReq,
  FeedbackReq,
  FeedbackRes
} from '../types/aimeApiType';
import { FetchEventSourceOptions } from '../types/aimeType';

const apis: Record<string, string> = {
  // 查询历史记录
  history: '//apigate.10jqka.com.cn/d/charge/llm-flow-engine/chat/v1/history',
  // 创建新的聊天对话
  create: '//apigate.10jqka.com.cn/d/charge/llm-flow-engine/chat/v1/create',
  // 对话
  chat: '//apigate.10jqka.com.cn/d/charge/llm-flow-engine/chat/v1/chat',
  // 反馈
  feedback: '//apigate.10jqka.com.cn/d/charge/llm-flow-engine/chat/v1/feedback'
};

// 获取聊天历史
const reqHistory = (params: HistoryReq): Promise<HistoryRes> =>
  customFetch({ url: apis.history, method: 'POST', data: params });

// 创建新的聊天对话
const createChat = (params: CreateReq): Promise<CreateRes> =>
  customFetch({ url: apis.create, method: 'POST', data: params });

// 聊天
const chat = (
  params: ChatReq,
  {
    onopen,
    onmessage,
    onerror,
    onclose
  }: Pick<FetchEventSourceOptions, 'onopen' | 'onmessage' | 'onerror' | 'onclose'>
) => sendSSE({ url: apis.chat, method: 'POST', data: params, onopen, onmessage, onerror, onclose });

// 反馈
const feedback = (params: FeedbackReq): Promise<FeedbackRes> =>
  customFetch({ url: apis.feedback, method: 'POST', data: params });
export { reqHistory, createChat, chat, feedback };
